package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.myapplication.FoodFragment;
import com.example.myapplication.InformFragment;
import com.example.myapplication.OrderFragment;
import com.example.myapplication.R;
import com.example.myapplication.UserFragment;

import org.litepal.tablemanager.Connector;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
    private ArrayList<Fragment> fragmentArrayList;
    private FoodFragment foodFragment;
    private InformFragment informFragment;
    private OrderFragment orderFragment;
    private UserFragment userFragment;
    private ImageButton btn_food;
    private ImageButton btn_inform;
    private ImageButton btn_order;
    private ImageButton btn_user;
    private TextView tv_food;
    private TextView tv_inform;
    private TextView tv_order;
    private TextView tv_user;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btn_food = findViewById(R.id.im_food);
        btn_inform = findViewById(R.id.im_inform);
        btn_order = findViewById(R.id.im_order);
        btn_user = findViewById(R.id.im_user);
        tv_food = findViewById(R.id.tv_food);
        tv_inform = findViewById(R.id.tv_inform);
        tv_order = findViewById(R.id.tv_order);
        tv_user = findViewById(R.id.tv_user);

        setListener();
        initFragment();

//        Connector.getDatabase();
    }

    private void setListener(){
        ClickListener onclick = new ClickListener();
        btn_food.setOnClickListener(onclick);
        btn_inform.setOnClickListener(onclick);
        btn_order.setOnClickListener(onclick);
        btn_user.setOnClickListener(onclick);
    }

    class ClickListener implements View.OnClickListener{

        @Override
        public void onClick(View view) {
            switch (view.getId()){
                case R.id.im_food:
                    changeFragment(0);
                    setImgBtnPressIcon(0);
                    break;
                case R.id.im_inform:
                    changeFragment(1);
                    setImgBtnPressIcon(1);
                    break;
                case R.id.im_order:
                    changeFragment(2);
                    setImgBtnPressIcon(2);
                    break;
                case R.id.im_user:
                    changeFragment(3);
                    setImgBtnPressIcon(3);
                    break;
            }
        }
    }

    private void changeFragment(int fragmentIndex){
        getSupportFragmentManager()
                .beginTransaction()
                .replace(R.id.id_content, fragmentArrayList.get(fragmentIndex))
                .commit();
    }

    private void initFragment(){
        fragmentArrayList = new ArrayList<>();
        foodFragment = new FoodFragment();
        informFragment = new InformFragment();
        orderFragment = new OrderFragment();
        userFragment = new UserFragment();

        fragmentArrayList.add(foodFragment);
        fragmentArrayList.add(informFragment);
        fragmentArrayList.add(orderFragment);
        fragmentArrayList.add(userFragment);
        getSupportFragmentManager()
                .beginTransaction()
                .add(R.id.id_content, fragmentArrayList.get(0))
                .commit();
    }

    private void setImgBtnPressIcon(int fragmentIndex){

        btn_food.setImageResource(R.drawable.main);
        btn_inform.setImageResource(R.drawable.talk);
        btn_order.setImageResource(R.drawable.order);
        btn_user.setImageResource(R.drawable.my);

        tv_food.setTextColor(getResources().getColor(R.color.colorTvNormal));
        tv_inform.setTextColor(getResources().getColor(R.color.colorTvNormal));
        tv_order.setTextColor(getResources().getColor(R.color.colorTvNormal));
        tv_user.setTextColor(getResources().getColor(R.color.colorTvNormal));

        switch (fragmentIndex){
            case 0:
                btn_food.setImageResource(R.drawable.main_act);
                tv_food.setTextColor(getResources().getColor(R.color.colorTvPress));
                break;
            case 1:
                btn_inform.setImageResource(R.drawable.talk_act);
                tv_inform.setTextColor(getResources().getColor(R.color.colorTvPress));
                break;
            case 2:
                btn_order.setImageResource(R.drawable.order_act);
                tv_order.setTextColor(getResources().getColor(R.color.colorTvPress));
                break;
            case 3:
                btn_user.setImageResource(R.drawable.my_act);
                tv_user.setTextColor(getResources().getColor(R.color.colorTvPress));
                break;
        }
    }


}